


<div id='GoodsCon_List' class='GoodsCon_Item' >
	<h2 style='text-align:center'>商品服务管理</h2>
	<div style='margin:5px 0;display:flex;height:26px;border:0px solid'>
		<div style='flex:1'>
			<input id='GoodsCon_SearchInp' type="text" style='border-radius:5px;width:100%;height:100%' />
		</div>
		<div id='GoodsCon_SearchBtn' style='border-radius:5px;width:40px;height:100%;box-sizing:border-box;padding:3px 10px;background:#eee'><img src='../Public/Image/search.png' style='height:20px' alt="" /></div>
	</div>

	<div>
		<select style='width:100%' id="GoodsCon_SelType" >
			<option value="6">全部</option>
			<option value="1">审核中</option>
			<option value="2">正常</option>
			<option value="3">未审过</option>
			<option value="4">已封禁</option>
			<option value="5">已下架</option>
		</select>
	</div>
	
	
	<div id='GoodsCon_ListArea'></div> 	<!--商品列表区-->

	<div id='GoodsCon_FpageArea'></div>	<!--分页区-->

</div>

<div id='GoodsCon_Add' class='GoodsCon_Item' style='display:none'>2</div><!--商品添加区-->

<div id='GoodsCon_Edit' class='GoodsCon_Item' style='display:none'>2</div><!--商品编辑区-->

<div style='text-align:center;margin:20px auto'>
	<button id='GoodsCon_AddBtn' onclick=GoodsCon_Add()>添加商品或服务</button>
</div>




<script>
var controGoods_pnow = 1;
var controGoods_psize = 5;		//每页条数

var controGoods_goodsList; //商品列表容器

GoodsCon_Init();

//点击搜索
$('#GoodsCon_SearchBtn').click(function(){
	controGoods_pnow = 1;
	GoodsCon_Init();
});

//商品状态切换
$('#GoodsCon_SelType').change(function(){
	controGoods_pnow = 1;
	GoodsCon_Init();
});


//type 是否清空容器数据，重新获取数据
function GoodsCon_Init(type){
	if(type){
		controGoods_goodsList = '';
	}
	if(controGoods_goodsList){
		C('goodsList from val');
		GoodsCon_GetGoodsListParse(controGoods_goodsList);		//解析列表
	}else{
		C('goodsList from after');
		GoodsCon_GetGoodsList(function(){		//获取商品列表
			GoodsCon_GetGoodsListParse(controGoods_goodsList);		//解析列表
		});
	}
}




//获取商品列表
function GoodsCon_GetGoodsList(fun){
	var data = {
		type:'getGoodsListForStoreId',
		store_id:accountInfo.id,
	};

	AjaxStore(data,function(res){
		var tmp = _checkJsonFormat(res,1); //检测JSON格式
		if(tmp.stat == 'errStat'){
			A('商户状态异常！');
		}
		if(tmp.stat == 'ok'){
			controGoods_goodsList = tmp.data
			fun();
		}

	});


}


//解析列表
function GoodsCon_GetGoodsListParse(res){
	if(typeof(res) != 'object' || res.length < 1){
		if(controGoods_pnow <=1){
			$('#GoodsCon_FpageArea').html('');
			$('#GoodsCon_ListArea').html('');
		}
	}else{
		$('#GoodsCon_ListArea').html('');
		var sel_type = parseInt($('#GoodsCon_SelType').find('option:selected').val());
		var search_val = $('#GoodsCon_SearchInp').val().trim();
		var info1 = res;

		var info = new Array();
		if(sel_type  == 6){
			if(search_val){
				for(var i=0;i<info1.length;i++){
					if(!info1[i].title.match(search_val)){
						continue;
					}else{
						info.push(info1[i]);
					}
				}
			}else{
				info = info1;
			}
		}else{
			for(var i=0;i<info1.length;i++){
				if(sel_type == info1[i].goods_stat){
					if(search_val && !info1[i].title.match(search_val)){
						continue;
					}else{
						info.push(info1[i]);
					}
				}
			}
		}
		var start = (controGoods_pnow-1) * controGoods_psize;
		var end = start + controGoods_psize;
		if(end > info.length){
			end = info.length;
		}
		var pnum = Math.ceil(info.length/controGoods_psize);

		var str = '';
		for(var i=start;i<end;i++){
			//根据id获取商品信息
			var goods_id = info[i].id;
			str += "<div style='display:flex;border-bottom:7px solid #fee;font-size:13px;margin:15px 0;'>";
			str += "<div style='border:1px solid #eef;width:100px;height:100px'>";
			//商品封面
			if(info[i].banner){
				var img_addr = upyun_shop_img+ChangeChar(info[i].banner,1);
			}else{
				var img_addr = base_img;
			}
			str += "<img src='"+img_addr+"' style='width:100%;height:100%' alt='图片呵呵了' ></div>";
			str += "<div style='flex:1;padding:0 5px;box-sizing:border-box;color:#999'>";
			//标题
			str += "<div style='font-size:18px;height:35px;word-break:break-all;overflow:hidden;color:#333'>"+ChangeChar(info[i].title,2)+"</div>";		
			//商品类型
			str += "<div style='flex:1;color:#9ab;font-weight:bold'>"+info[i].type_set+"</div>";
			str += "<div style='height:20px;display:flex;margin:8px 0'>";
			//价格
			str += "<div style='flex:1;color:red;font-weight:bold'>￥："+info[i].price+"</div>";
			//编号
			str += "<div style='flex:1;text-align:right'>编号："+info[i].number+"</div></div>";
			str += "<div style='height:20px;display:flex'>";
			//状态
			switch(info[i].goods_stat){
				case 2:case '2':var stat_str = '<text style="color:blue">正常</text>';break;
				case 3:case '3':var stat_str = '审核未过';break;
				case 4:case '4':var stat_str = '已封禁';break;
				case 5:case '5':var stat_str = '已下架';break;
				default :
					var stat_str = '审核中';break;
			}
			str += "<div style='width:60px'>"+stat_str+"</div>";

			str += "<div style='flex:1;text-align:right'>";
			//封禁的商品不可操作
			if(info[i].goods_stat != 4){
				//2为上架，5为下架
				if(info[i].goods_stat == 2 || info[i].goods_stat == 5){
					var text = info[i].goods_stat== 2?'下架':'上架';
					str += "<span style='padding:1px 7px;background:#5ee;color:#fff;border-radius:5px' onclick=_goodsUpOrDown("+goods_id+","+info[i].goods_stat+")>"+text+"</span>　";
				}

				if(!info[i].free_id){
					str += "<span style='padding:1px 7px;background:#aaf;color:#fff;border-radius:5px' onclick='GoodsCon_Free("+goods_id+","+info[i].goods_stat+","+info[i].free_id+")'>生成砍价</span>　";
					str += "<span style='padding:1px 7px;background:#aaf;color:#fff;border-radius:5px' onclick='GoodsCon_Edit("+goods_id+")'>编辑</span>　";
					str += "<span style='padding:1px 7px;background:#aaa;color:#fff;border-radius:5px' onclick='GoodsCon_Del("+goods_id+","+info[i].free_id+")'>删除</span>";
				}
			}
			str += "</div></div></div></div>";
		}

		$('#GoodsCon_ListArea').html(str);

		var max = controGoods_pnow+1 < pnum?controGoods_pnow+1:pnum;
		Fpage(controGoods_pnow,max,function(p){
			controGoods_pnow = p;
			GoodsCon_Init();
		},1,1,'#GoodsCon_FpageArea');

	}
}


	//商品的上架下架
	function _goodsUpOrDown(id,stat){
		if(!confirm('确认操作？')){
			return;
		}
		var data = {
			type:'goodsUpOrDown',
			id:id,
			stat:stat
		};
		AjaxStore(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat=='ok'){
				A('操作成功！');
				GoodsCon_Init(1);
			}else{
				A('操作失败！');
			}
		});
	}


	//商品的删除 
	function GoodsCon_Del(id,free_id){
		if(free_id){
			A('关联砍价商品暂无法删除2！');return;
		}

		// if(!_confirm()){
		// 	return;
		// }
		var imgDirName = imgRootDir + "/" + cityInfo[0] + "/" + accountInfo.account + '/' +id;
		Notice('删除中......');
		var data = {
			type:'storeGoodsDel',
			store_id:accountInfo.id,
			goods_id:id,
			imgDirName:imgDirName
		};
		AjaxStore(data,function(res){
			var tmp = _checkJsonFormat(res);
			Notice('');
			switch(tmp.stat){
				case 'ok':
					A('操作成功！');
					GoodsCon_Init(1);break;
				case 'err2':A('关联砍价商品暂无法删除！');break;
				case 'err3':A('操作异常！');break;
			}
		});
	}


	//添加商品
	function GoodsCon_Add(id){
		$('.GoodsCon_Item').hide();
		$('#GoodsCon_AddBtn').hide();
		Ajax('',function(res){
			$('#GoodsCon_Add').html(res).show();
		},'Template/controlGoodsAdd.html');
	}


	//编辑商品
	function GoodsCon_Edit(goods_id){
		sessionStorage.goods_edit_id = goods_id;
		$('.GoodsCon_Item').hide();
		$('#GoodsCon_AddBtn').hide();
		Ajax('',function(res){
			$('#GoodsCon_Edit').html(res).show();
		},'Template/controlGoodsEdit.html');
	}



	//生成砍价商品
	function GoodsCon_Free(goods_id,goods_stat,free_id){
		if(goods_stat != 2){
			A('商品状态非正常！');return;
		}
		if(free_id){
			//A('不得重复生成砍价商品！');return;
		}
		if(!confirm('生成的砍价商品删除前不能修改和删除该商品！')){
			return;
		}
		var data = {
			type:'freeGoodsAdd',
			goods_id:goods_id,
			store_id:accountInfo.id,
			goods_stat:goods_stat
		}
		AjaxStore(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('砍价商品创建成功！');
				GoodsCon_Init(1);
			}else{
				if(tmp.stat == 'err3'){
					A('商品未通过审核！');
				}else{
					A('操作异常或砍价商品已存在！');
				}
			}
		});
	}
</script>